package com.xtravel.discovery.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.webkit.*;
import android.widget.*;
import com.xtravel.R;

@SuppressLint("SetJavaScriptEnabled")
public class BrowserActivity extends Activity {
    private WebView browser;
    private FrameLayout shareFragmentLayout;
    private String url;
    private TextView textRefresh;
    private RelativeLayout container_panel;
    private ProgressBar pgrBar;
    private Animation hideAnimation, displayAnimation;
    private RelativeLayout menuCaption;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.browser_main_page);
        this.browser = (WebView) findViewById(R.id.browser);
        this.textRefresh = (TextView) findViewById(R.id.share);
        this.shareFragmentLayout = (FrameLayout) findViewById(R.id.shareFrameLayout);
        this.container_panel = (RelativeLayout) findViewById(R.id.container_panel);
        this.pgrBar = (ProgressBar) findViewById(R.id.myProgressBar);
        this.menuCaption = (RelativeLayout)findViewById(R.id.menuBar);
        Intent intent = getIntent();
        this.url = intent.getStringExtra("url");
        initBrowser(browser);
        browser.loadUrl(url);

        this.container_panel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fadeInCaption();
            }
        });

        autoHideCaption(1000);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @SuppressLint("NewApi")
    private void initBrowser(WebView browser) {
        browser.setWebViewClient(client);
        browser.setWebChromeClient(chromeClient);
        final WebSettings settings = browser.getSettings();
        settings.setSupportZoom(false);
        settings.setUseWideViewPort(true);
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setBuiltInZoomControls(false);
        settings.setDisplayZoomControls(false);
        browser.requestFocusFromTouch();
        textRefresh.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                onRefresh(view);
            }
        });
    }

    public void onBack(View view) {
         finish();
    }

    public void onRefresh(View view) {
        browser.loadUrl(this.url);
    }

    private WebViewClient client = new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    };

    private void autoHideCaption(int duration)
    {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                fadeOutCaption();
            }
        }, duration);
    }

    private void fadeOutCaption(){
        if( null != hideAnimation ){
            hideAnimation.cancel( );
        }

        hideAnimation = new AlphaAnimation(1.0f, 0.0f);
        hideAnimation.setDuration(3000);
        hideAnimation.setFillAfter( true );
        menuCaption.startAnimation(hideAnimation);
    }

    private void fadeInCaption(){
        if( null != displayAnimation ){
            displayAnimation.cancel( );
        }

        displayAnimation = new AlphaAnimation(0.0f, 1.0f);
        displayAnimation.setDuration(3000);
        displayAnimation.setFillAfter( true );
        menuCaption.startAnimation(displayAnimation);
        autoHideCaption(5000);
    }

    private WebChromeClient chromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                pgrBar.setVisibility(View.INVISIBLE);
            } else {
                if (View.INVISIBLE == pgrBar.getVisibility()) {
                    pgrBar.setVisibility(View.VISIBLE);
                }
                pgrBar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }
    };
}
